package com.dbb

import org.springframework.web.multipart.MultipartHttpServletRequest

class ImageController {

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        [imageInstanceList: Image.list(params), imageInstanceTotal: Image.count()]
    }

    def create = {
        def imageInstance = new Image()
        imageInstance.properties = params
        return [imageInstance: imageInstance]
    }

    def save = {
        def imageInstance = new Image(params)
        if (!imageInstance.hasErrors() && imageInstance.save()) {
			rawUpload()
            flash.message = "image.created"
            flash.args = [imageInstance.id]
            flash.defaultMessage = "Image ${imageInstance.id} created"
            redirect(action: "show", id: imageInstance.id)
        }
        else {
            render(view: "create", model: [imageInstance: imageInstance])
        }
    }	

    def show = {
        def imageInstance = Image.get(params.id)
        if (!imageInstance) {
            flash.message = "image.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Image not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [imageInstance: imageInstance]
        }
    }

    def edit = {
        def imageInstance = Image.get(params.id)
        if (!imageInstance) {
            flash.message = "image.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Image not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [imageInstance: imageInstance]
        }
    }

    def update = {
        def imageInstance = Image.get(params.id)
        if (imageInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (imageInstance.version > version) {
                    
                    imageInstance.errors.rejectValue("version", "image.optimistic.locking.failure", "Another user has updated this Image while you were editing")
                    render(view: "edit", model: [imageInstance: imageInstance])
                    return
                }
            }
            imageInstance.properties = params
            if (!imageInstance.hasErrors() && imageInstance.save()) {
                flash.message = "image.updated"
                flash.args = [params.id]
                flash.defaultMessage = "Image ${params.id} updated"
                redirect(action: "show", id: imageInstance.id)
            }
            else {
                render(view: "edit", model: [imageInstance: imageInstance])
            }
        }
        else {
            flash.message = "image.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Image not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }
	
	def renderImage = {
		def imageInstance = Image.get(params.id)
		if (imageInstance?.photo) {
			response.setContentLength(imageInstance.photo.length)
			response.outputStream.write(imageInstance.photo)
		} else {
			response.sendError(404)
		}
	}
	
	def rawUpload = {
		// a Spring 		
		def mhsr = request.getFile('photo')
		if (!mhsr?.empty) {
		   mhsr.transferTo(
			  new File(
				 grailsApplication.config.basefile.path + "/hubbub/images/" + mhsr.getOriginalFilename()
			  )
		   )
		}
	}

    def delete = {
		hdImageService.
        def imageInstance = Image.get(params.id)
        if (imageInstance) {
            try {
                imageInstance.delete()
                flash.message = "image.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Image ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "image.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Image ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "image.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Image not found with id ${params.id}"
            redirect(action: "list")
        }
    }
	
}
